/*
 * @lc app=leetcode id=349 lang=cpp
 *
 * [349] Intersection of Two Arrays
 */
class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {

        vector<int> ret;
        int len1 = nums1.size();
        int len2 = nums2.size();
        if (len1 < 1 || len2 < 1) {
            return ret;
        }

        sort(nums1.begin(), nums1.end());
        sort(nums2.begin(), nums2.end());

        int i = 0, j = 0;
        bool start = true;
        int last = 0;
        while (i < len1 && j < len2) {

            if (nums1[i] < nums2[j]) {
                i++;
            } else if (nums1[i] > nums2[j]) {
                j++;
            } else {
                if (start) {
                    last = nums1[i];
                    ret.push_back(last);
                    start = false;
                } else {
                    if (nums1[i] != last) {
                        last = nums1[i];
                        ret.push_back(last);
                    }
                }
                i++;
                j++;
            }
        }
        return ret;
        
    }
};

